# encoding = utf-8
class VouchersController < ApplicationController
  
  before_filter :authenticate_admin!, :only => [:new, :create, :list, :edit, :update, :delete]

  def new
    @voucher = Voucher.new
    @action = {:action => 'create'}
    @action_title = '添加新代金券'
    render :layout => 'admins'
  end

  def create
    voucher = Voucher.new(params[:voucher])

    if voucher.save
      flash[:success] = '添加代金券成功'
    else
      flash[:error] = '添加代金券失败'
    end

    redirect_to :action => "list"
  end

  def list
    @vouchers = Voucher.order('created_at desc').paginate :page => params[:page], :per_page => PAGE_SIZE
    render :layout => 'admins'
  end

  def edit
    @voucher = Voucher.find(params[:id])
    @action = {:action => 'update', :id => @voucher.id}
    @action_title = '编辑代金券'
    render :layout => 'admins', :action => 'new'
  end

  def update
    voucher = Voucher.update(params[:id], params[:voucher])
    if voucher
      flash[:success] = '代金券更新成功'
    else
      flash[:error] = '代金券更新失败'
    end
    redirect_to :action => "list"
  end

  def delete
    voucher = Voucher.find(params[:id])
    if voucher.destroy
      flash[:success] = '代金券删除成功'
    end
    redirect_to :action => "list"
  end
  
end
